Source for file class.inputfilter.php
Documentation is available at class.inputfilter.php
/** @class: InputFilter (PHP4 & PHP5, with comments)
* @project: PHP Input Filter
* @version: 1.2.2_php4/php5
* @contributors: Gianpaolo Racca, Ghislain Picard, Marco Wandschneider, Chris Tobin and Andrew Eddie.
* @copyright: Daniel Morris
* @email: dan@rootcube.com
* @license: GNU General Public License (GPL)
var $tagBlacklist =
array('applet', 'body', 'bgsound', 'base', 'basefont', 'embed', 'frame', 'frameset', 'head', 'html', 'id', 'iframe', 'ilayer', 'layer', 'link', 'meta', 'name', 'object', 'script', 'style', 'title', 'xml');
var $attrBlacklist =
array('action', 'background', 'codebase', 'dynsrc', 'lowsrc'); // also will strip ALL event handlers
* Constructor for inputFilter class. Only first parameter is required.
* @param Array $tagsArray - list of user-defined tags
* @param Array $attrArray - list of user-defined attributes
* @param int $tagsMethod - 0= allow just user-defined, 1= allow all but user-defined
* @param int $attrMethod - 0= allow just user-defined, 1= allow all but user-defined
* @param int $xssAuto - 0= only auto clean essentials, 1= allow clean blacklisted tags/attr
function inputFilter($tagsArray =
array(), $attrArray =
array(), $tagsMethod =
0, $attrMethod =
0, $xssAuto =
1) {
// make sure user defined arrays are in lowercase
for ($i =
0; $i <
count($tagsArray); $i++
) $tagsArray[$i] =
strtolower($tagsArray[$i]);
for ($i =
0; $i <
count($attrArray); $i++
) $attrArray[$i] =
strtolower($attrArray[$i]);
* Method to be called by another php script. Processes for XSS and specified bad code.
* @param Mixed $source - input string/array-of-string to be 'cleaned'
* @return String $source - 'cleaned' version of input parameter
// clean all elements in this array
foreach($source as $key =>
$value)
// filter element for XSS and other 'bad' code etc.
// filter source for XSS and other 'bad' code etc.
// return parameter as given
* Internal method to iteratively remove all unwanted tags and attributes
* @param String $source - input string to be 'cleaned'
* @return String $source - 'cleaned' version of input parameter
// provides nested-tag protection
* Internal method to strip a string of certain tags
* @param String $source - input string to be 'cleaned'
* @return String $source - 'cleaned' version of input parameter
// find initial tag's position
$tagOpen_start =
strpos($source, '<');
// interate through string until no tags left
while($tagOpen_start !==
FALSE) {
// process tag interatively
$preTag .=
substr($postTag, 0, $tagOpen_start);
$postTag =
substr($postTag, $tagOpen_start);
$fromTagOpen =
substr($postTag, 1);
$tagOpen_end =
strpos($fromTagOpen, '>');
if ($tagOpen_end ===
false) break;
// next start of tag (for nested tag assessment)
$tagOpen_nested =
strpos($fromTagOpen, '<');
if (($tagOpen_nested !==
false) &&
($tagOpen_nested <
$tagOpen_end)) {
$preTag .=
substr($postTag, 0, ($tagOpen_nested+
1));
$postTag =
substr($postTag, ($tagOpen_nested+
1));
$tagOpen_start =
strpos($postTag, '<');
$tagOpen_nested =
(strpos($fromTagOpen, '<') +
$tagOpen_start +
1);
$currentTag =
substr($fromTagOpen, 0, $tagOpen_end);
$tagLength =
strlen($currentTag);
$tagOpen_start =
strpos($postTag, '<');
// iterate through tag finding attribute pairs - setup
$currentSpace =
strpos($tagLeft, ' ');
if (substr($currentTag, 0, 1) ==
"/") {
list
($tagName) =
explode(' ', $currentTag);
$tagName =
substr($tagName, 1);
list
($tagName) =
explode(' ', $currentTag);
// excludes all "non-regular" tagnames OR no tagname OR remove if xssauto is on and tag is blacklisted
$postTag =
substr($postTag, ($tagLength +
2));
$tagOpen_start =
strpos($postTag, '<');
// this while is needed to support attribute values with spaces in!
while ($currentSpace !==
FALSE) {
$fromSpace =
substr($tagLeft, ($currentSpace+
1));
$nextSpace =
strpos($fromSpace, ' ');
$openQuotes =
strpos($fromSpace, '"');
$closeQuotes =
strpos(substr($fromSpace, ($openQuotes+
1)), '"') +
$openQuotes +
1;
if (strpos($fromSpace, '=') !==
FALSE) {
// opening and closing quotes exists
if (($openQuotes !==
FALSE) &&
(strpos(substr($fromSpace, ($openQuotes+
1)), '"') !==
FALSE))
$attr =
substr($fromSpace, 0, ($closeQuotes+
1));
else $attr =
substr($fromSpace, 0, $nextSpace);
} else $attr =
substr($fromSpace, 0, $nextSpace);
if (!$attr) $attr =
$fromSpace;
// add to attribute pairs array
$currentSpace =
strpos($tagLeft, ' ');
// appears in array specified by user
// remove this tag on condition
// reconstruct tag with allowed attributes
$preTag .=
'<' .
$tagName;
for ($i =
0; $i <
count($attrSet); $i++
)
$preTag .=
' ' .
$attrSet[$i];
// reformat single tags to XHTML
if (strpos($fromTagOpen, "</" .
$tagName)) $preTag .=
'>';
} else $preTag .=
'</' .
$tagName .
'>';
$postTag =
substr($postTag, ($tagLength +
2));
$tagOpen_start =
strpos($postTag, '<');
// append any code after end of tags
* Internal method to strip a tag of certain attributes
for ($i =
0; $i <
count($attrSet); $i++
) {
// skip blank spaces in tag
if (!$attrSet[$i]) continue;
// split into attr name and value
list
($attrSubSet[0]) =
explode(' ', $attrSubSet[0]);
// removes all "non-regular" attr names AND also attr blacklisted
// xss attr value filtering
// strips unicode, hex, etc
$attrSubSet[1] =
str_replace('&#', '', $attrSubSet[1]);
// strip normal newline within attr value
// [requested feature] convert single quotes from either side to doubles (Single quotes shouldn't be used to pad attr value)
if ((substr($attrSubSet[1], 0, 1) ==
"'") &&
(substr($attrSubSet[1], (strlen($attrSubSet[1]) -
1), 1) ==
"'"))
$attrSubSet[1] =
substr($attrSubSet[1], 1, (strlen($attrSubSet[1]) -
2));
// auto strip attr's with "javascript:
// if matches user defined array
// keep this attr on condition
if ($attrSubSet[1]) $newSet[] =
$attrSubSet[0] .
'="' .
$attrSubSet[1] .
'"';
// attr has decimal zero as value
else if ($attrSubSet[1] ==
"0") $newSet[] =
$attrSubSet[0] .
'="0"';
// reformat single attributes to XHTML
else $newSet[] =
$attrSubSet[0] .
'="' .
$attrSubSet[0] .
'"';
* Function to determine if contents of an attribute is safe
* @param Array A 2 element array for attribute [name] and [value]
* @return Boolean True if bad code is detected
((strpos($attrSubSet[1], 'expression') !==
false) &&
($attrSubSet[0]) ==
'style') ||
(strpos($attrSubSet[1], 'javascript:') !==
false) ||
(strpos($attrSubSet[1], 'behaviour:') !==
false) ||
(strpos($attrSubSet[1], 'vbscript:') !==
false) ||
(strpos($attrSubSet[1], 'mocha:') !==
false) ||
(strpos($attrSubSet[1], 'livescript:') !==
false)
* Try to convert to plaintext
$source =
preg_replace('/&#(\d+);/me',"chr(\\1)", $source); // decimal notation
$source =
preg_replace('/&#x([a-f0-9]+);/mei',"chr(0x\\1)", $source); // hex notation
* Method to be called by another php script. Processes for SQL injection
* @param Mixed $source - input string/array-of-string to be 'cleaned'
* @param Buffer $connection - An open MySQL connection
* @return String $source - 'cleaned' version of input parameter
function safeSQL($source, &$connection) {
// clean all elements in this array
foreach($source as $key =>
$value)
// filter element for SQL injection
// filter source for SQL injection
// return parameter as given
* @param Resource $connection - An open MySQL connection
// quote both numeric and text
* @param Resource $connection - An open MySQL connection
else mysql_real_escape_string($string);
Documentation generated on Mon, 05 May 2008 16:17:10 +0400 by phpDocumentor 1.4.0